﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using FNSShoppingCart;
public partial class Pages_Login : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Attempt > 1)
        {
            var captcha = Login1.FindControl("RecaptchaControl") as Recaptcha.RecaptchaControl;
            captcha.Enabled = true;
            captcha.Visible = true;
        }
        //if (!Page.IsPostBack)
        //{
        //    if (Page.Request.UrlReferrer != null)
        //    {
        //        Session["LoginReferrer"] = Page.Request.UrlReferrer.ToString();
        //    }
        //}
    }

   
    protected void Login_Authenticate(object sender, AuthenticateEventArgs e)
    {
        Attempt++;
        string loginUsername = Login1.UserName.Trim();
        string loginPassword = Login1.Password.Trim();

        var captcha = Login1.FindControl("RecaptchaControl") as Recaptcha.RecaptchaControl;
        captcha.Validate();

        //First, check if CAPTCHA matches up
        if (!captcha.IsValid)
        {
            //CAPTCHA invalid
            Login1.FailureText = Attempt == 3 ? "" : "Codul  pe care l-aţi introdus nu se potriveste cu imaginea furnizate, vă rugăm să încercaţi din nou cu aceasta noua imagine.";
            e.Authenticated = false;
        }
        else
        {
            //Next, determine if the user's username/password are valid
            if (Membership.ValidateUser(loginUsername, loginPassword))
            {
                //Only set e.Authenticated to True if ALL checks pass
                e.Authenticated = true;
                Attempt = 0;
              //  Session["ip_address"] = Request.Params["REMOTE_ADDR"]+"";

                var url = "~/"+Page.RouteData.Values["ReturnUrl"] as string;

                Login1.DestinationPageUrl = url;
            }
            else
            {
                e.Authenticated = false;
                Login1.FailureText = "Numele de utilizator si/sau parola sunt invalide.";
            }
        }

    }

    private int Attempt
    {
        get
        {
            object o = Session["attempt"];
            int attempt = (o == null) ? 0 : Convert.ToInt32(o);
            return attempt;
        }
        set
        {
            Session["attempt"] = value;
        }
    }
    //protected void Login1_LoggedIn(object sender, EventArgs e)
    //{
    //    var url = Page.RouteData.Values["ReturnUrl"] as string;
    //    FormsAuthentication.SetAuthCookie(url, true);
    //    Response.RedirectPermanent(url);

    //}
    //protected void LoginButton_Click(object sender, EventArgs e)
    //{

    //    if (Membership.ValidateUser(Login1.UserName, Login1.Password))
    //    {
    //        Response.Write(FormsAuthentication.DefaultUrl);// = Page.GetRouteUrl("Home", new { }) as string;
    //        FormsAuthentication.RedirectFromLoginPage(Login1.UserName, Login1.RememberMeSet);
    //    }
    //    else
    //        Login1.FailureText = "Login failed. Please check your user name and password and try again.";
    //}

    protected void Login1_LoggedIn(object sender, EventArgs e)
    {
        MyShoppingCart usersShoppingCart = new MyShoppingCart();
        String cartId = usersShoppingCart.GetShoppingCartId();
        var user = Membership.GetUser(Login1.UserName);
        usersShoppingCart.MigrateCart(cartId, user.ProviderUserKey.ToString());

        //if (Session["LoginReferrer"] != null)
        //{
        //    Response.Redirect(Session["LoginReferrer"].ToString());
        //}

        Session["UserName"] = user.ProviderUserKey.ToString();
    }
}